﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JetBrains.Annotations;
using Orchard.Data;
using Hub.Models;

namespace Hub.Services {
    [UsedImplicitly]
    public class HubService : IHubService {

        private readonly IRepository<ShareContentPartRecord> _shareContentRepository;

        public HubService(IRepository<ShareContentPartRecord> shareContentRepository) {
            _shareContentRepository = shareContentRepository;
        }

        public IEnumerable<ShareContentPartRecord> RetriveAll() {
            return _shareContentRepository.Table
                .OrderByDescending(i => i.Id)
                .ToList();
        }

        public ShareContentPartRecord RetriveMapping(int id) {
                var result = _shareContentRepository
                    .Fetch(c => c.Id == id)
                    .FirstOrDefault();

                return result ?? null;
        }

        public void PublishContent(ShareContentPartRecord content) {

            if (content != null) {
                var result = _shareContentRepository
                    .Fetch(c => c.PictureUrl.Equals(content.PictureUrl))
                    .FirstOrDefault();

                if (result == null) {
                    _shareContentRepository.Create(content);
                }
            }
        }

        public void UpdateContent(ShareContentPartRecord content) {
            _shareContentRepository.Update(content);
        }

        public void DeleteContent(ShareContentPartRecord content) {
            _shareContentRepository.Delete(content);
        }
    }
}
